package com.sim.action;



import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.sim.base.BaseAction;
import com.sim.domain.Subsidize;
import com.sim.domain.SubsidizeType;
import com.sim.util.QueryHelper;
@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class SubsidizeMAction extends BaseAction<Subsidize> {
	private Long subsidizeTypeId;
	/**
	 * true 表示升序<br>
	 * false 表示降序
	 */
	private boolean asc = false;
	//主页面
    public String list() throws Exception {
    	new QueryHelper(Subsidize.class, "s")//
    	    .addOrderProperty(true, "s.currentTime", asc) //  
    	    .preparePageBean(subsidizeService, pageNum, pageSize);		    
       
    	return "list";
    }
    /** 删除 */
	public String delete() throws Exception {
		subsidizeService.delete(model.getId());
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		// 准备回显的数据
		List<SubsidizeType> subsidizeTypeList = subsidizeTypeService.findAll();
		ActionContext.getContext().put("subsidizeTypeList", subsidizeTypeList);

		return "saveUI";
	}

	/** 添加 */
	public String add() throws Exception {
		SubsidizeType subsidizeType = subsidizeTypeService.getById(subsidizeTypeId);
		model.setSubsidizeType(subsidizeType);
		subsidizeService.save(model);
		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		List<SubsidizeType> subsidizeTypeList = subsidizeTypeService.findAll();
		ActionContext.getContext().put("subsidizeTypeList", subsidizeTypeList);
		// 准备回显的数据
		subsidizeTypeId = model.getId();
		Subsidize subsidize = subsidizeService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(subsidize);
		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		// 1，从数据库中取出原对象
		Subsidize subsidize = subsidizeService.getById(model.getId());
		// 2，设置要修改的属性
		subsidize.setTitle(model.getTitle());
        subsidize.setContent(model.getContent());
        subsidize.setCurrentTime(new Date());
        subsidize.setLeader(model.getLeader());
		// 3，更新到数据库
		subsidizeService.update(subsidize);

		return "toList";
	}
	
	
	//===================
	
	public Long getSubsidizeTypeId() {
		return subsidizeTypeId;
	}
	public void setSubsidizeTypeId(Long subsidizeTypeId) {
		this.subsidizeTypeId = subsidizeTypeId;
	}
}
